package cn.edu.nju.software.android.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.nju.software.android.adapter.PersonalStatusListViewAdapter;
import cn.edu.nju.software.android.model.User;

import me.maxwin.view.IListviewCallback;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;

public class DetailedUserActivity extends Activity  implements IXListViewListener, IListviewCallback{
	private User user;
	private XListView listView;
	private PersonalStatusListViewAdapter adapter;
	private Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		user = (User)intent.getSerializableExtra("user");
		setContentView(R.layout.layout_user);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE) ;
		actionBar.setTitle(user.getName());
		//		actionBar.setIcon(R.drawable.back);

		listView = (XListView) findViewById(R.id.user);
		adapter = new PersonalStatusListViewAdapter(user,this);
		/*listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if(position != 1){
					Intent intent = new Intent(DetailedUserActivity.this, DetailedWeiboActivity.class);
					intent.putExtra("status", adapter.getItem(position-2));
					intent.putExtra("parent", DetailedUserActivity.class);
					intent.putExtra("user", user);
					startActivityForResult(intent, 1);
				}
			}
		});*/
		listView.setAdapter(adapter);
		listView.setXListViewListener(this);
		listView.setPullRefreshEnable(false);
		listView.setPullLoadEnable(false);
		adapter.loadMoreStatuses();
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case android.R.id.home:
			finish();
			break;
		}
		return false;
	}


	@SuppressLint("SimpleDateFormat")
	@Override
	public void onComplete() {
		listView.stopLoadMore();
		Date timeNow = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		String time = format.format(timeNow);
		listView.setRefreshTime(time);		
	}

	@Override
	public void disableLoadFunction() {
		listView.setPullLoadEnable(false);		
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				adapter.loadMoreStatuses();
			}
		}, 2000);		
	}

	@Override
	public void enableLoadFunction() {
		listView.setPullLoadEnable(true);
	}


	@Override
	public void resetToTopRow() {
		// TODO Auto-generated method stub

	}
}
